﻿using Microsoft.AspNetCore.Mvc;
using SenGe.Domain;
using System.Security.Claims;

namespace SenGe.Web.Controllers
{
    /// <summary>
    /// 基础控制器
    /// </summary>
    public class BaseController : Controller
    {
        /// <summary>
        /// 获取当前登录用户信息
        /// </summary>
        /// <returns></returns>
        protected DtoCurrentUser? GetCurrentUserInfo()
        {
            if (User == null || User.Identity == null || !User.Identity.IsAuthenticated) return null;
            // 从Claims中获取用户信息
            string? id = User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "0";
            string? name = User.FindFirst(ClaimTypes.Name)?.Value ?? "未知用户";
            DtoCurrentUser dtoCurrentUser = new DtoCurrentUser()
            {
                Id = long.Parse(id),
                Name = name
            };
            if (User.FindFirst("Avatar")?.Value?.Length > 0) dtoCurrentUser.Avatar = User.FindFirst("Avatar")?.Value;
            return dtoCurrentUser;
        }
    }
}
